<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_mayitwc/model')->方法名();
*/
namespace jy_mayitwc;
defined('BY_JYA') or exit('error');
require_once ROOT_PLUGIN.'jy_mayitwc/twc/vendor/autoload.php';
use \AntChain\TWC\Client as AntChainTWCClient;
use \AntChain\TWC\Models\Config;
use \AntChain\TWC\Models\CreateLeaseNotifyregisterRequest;
use \AntChain\TWC\Models\CreateLeaseBizRequest;
use \AntChain\TWC\Models\CreateLeaseBiznotaryRequest;
class model extends \plugin{
	/**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return AntChainTWCClient Client
     */
    public static function createClient(){
        $config = new Config([]);
		$setting=plugin('jy_mayitwc')->plugin_setting('basic');
		if(!$setting['appid'] || !$setting['secret']){
			exi('请联系管理员，设置基本参数！','error');
		}
        // 您的AccessKey ID
        $config->accessKeyId = $setting['appid'];
        // 您的AccessKey Secret
        $config->accessKeySecret = $setting['secret'];
        return new AntChainTWCClient($config);
    }
	/*
		通知注册
	*/
	public function upticket(){
		$setting=plugin('jy_mayitwc')->plugin_setting('basic');
		$client = self::createClient();
        $createLeaseNotifyregisterRequest = new CreateLeaseNotifyregisterRequest([
			"productInstanceId"=>$setting['istest']?'lease-api-sandbox':'lease-api-prod',
		]);
        $result=$client->createLeaseNotifyregister($createLeaseNotifyregisterRequest);
		$result=(array)$result;
		//print_r($result);exit;
		if($result['resultCode']!='OK'){
			exi('通知注册失败，'.$result['resultCode'].':'.$result['resultMsg'].',reqMsgId:'.$result['reqMsgId'],'error');
		}
		if($result['code']){
			exi('通知注册失败，'.$result['code'].',err_message:'.$result['err_message'],'error');
		}
		return $result['data'];		
	}
	/*
		3.1租赁平台统一接口服务twc.notary.lease.biz.create
	*/
	public function upinfo($bizContent=array(),$type='order',$operationType='INSERT'){
		if(!$bizContent){
			exi('请传入要上链的内容','error');
		}
		$type=$type?:'order';
		$operationType=$operationType?:'INSERT';
		$client = self::createClient();
		$setting=plugin('jy_mayitwc')->plugin_setting('basic');
		$createLeaseBizRequest = new CreateLeaseBizRequest([
			"productInstanceId"=>$setting['istest']?'lease-api-sandbox':'lease-api-prod',
            "bizContent" => ijson_encode($bizContent),
            "operationType" => $operationType,
            "type" => $type
        ]);
		//file_put_contents(ROOT_D.'/5.txt',ijson_encode($bizContent));
        $result=$client->createLeaseBiz($createLeaseBizRequest);
		$result=(array)$result;
		//print_r($result);exit;
		$data=array(
			'type'=>$type,
			'operationtype'=>$operationType,
			'bizcontent'=>$bizContent,
		);
		if($result['resultCode']!='OK' && $result['resultCode']!=200){
			$data['passed']=2;
			model('jy_mayitwc/member')->addlog($data);
			exi($type.'上链失败，'.$result['resultCode'].':'.$result['resultMsg'].',reqMsgId:'.$result['reqMsgId'],'error');
		}
		if($result['code'] && $result['code']!=200){
			$data['passed']=2;
			model('jy_mayitwc/member')->addlog($data);
			exi($type.'上链失败，'.$result['code'].',err_message:'.$result['err_message'],'error');
		}
		$data['passed']=1;
		$data['response_data']=$result['response_data'];
		model('jy_mayitwc/member')->addlog($data);
		return $result['response_data'];		
	}
	/*
		3.3租赁平台存证文件哈希twc.notary.lease.biznotary.create
	*/
	public function biznotary($hash=''){
		if(!$hash){
			exi('请传入文件哈希','error');
		}
		$type=$type?:'order';
		$operationType=$operationType?:'INSERT';
		$client = self::createClient();
		$setting=plugin('jy_mayitwc')->plugin_setting('basic');
		$createLeaseBiznotaryRequest = new CreateLeaseBiznotaryRequest([
			"productInstanceId"=>$setting['istest']?'lease-api-sandbox':'lease-api-prod',
            "leaseCorpOwnerName" => $setting['lease_corp_owner_name'],
            "leaseCorpId" => $setting['lease_corp_id'],
            "leaseCorpName" => $setting['lease_corp_name'],
            "hash" => $hash
        ]);
        $result=$client->createLeaseBiznotary($createLeaseBiznotaryRequest);
		$result=(array)$result;
		//print_r($result);exit;
		$bizContent['hash']=$hash;
		$data=array(
			'type'=>$type,
			'operationtype'=>$operationType,
			'bizcontent'=>$bizContent,
		);
		if($result['resultCode']!=200){
			$data['passed']=2;
			model('jy_mayitwc/member')->addlog($data);
			exi('存证失败，'.$result['resultCode'].':'.$result['resultMsg'].',reqMsgId:'.$result['reqMsgId'],'error');
		}
		if($result['code']){
			$data['passed']=2;
			model('jy_mayitwc/member')->addlog($data);
			exi('存证失败，'.$result['code'].',err_message:'.$result['err_message'],'error');
		}
		$data['passed']=1;
		$data['response_data']=$result['responseData'];
		model('jy_mayitwc/member')->addlog($data);
		return $hash;		
	}
}
?>